package com.sun.config;

import android.os.Build;
import android.os.SystemProperties;
import android.text.TextUtils;

import com.sun.DemoApplication;
import com.sun.dailyprj.BuildConfig;
import com.sun.dailyprj.util.Logger;

public class DeviceInfo{
    public AppConfig mAppConfig;
    public final static String APPCONFIG_FILE = "RockGota";
    final static String DEF_VAL = "unknown";

    public final static int DELAY_TIME = 10;//min
    private final static int REPEAT_TIME = 1 * 60;//min
    private final static int PERIOD_TIME = 24 * 60;//hour
    public final static int START_HOUR = 0;
    public final static int END_HOUR = 5;
    public final static int DELAY_NEEDED = 5;//min，留时间共系统检测IDLE状态

    public DeviceInfo() {
        AppConfig.LogConfig log = new AppConfig.LogConfig();
        mAppConfig = new AppConfig(APPCONFIG_FILE, DemoApplication.mContext, log);
        log.enabled = mAppConfig.debugIsOn();
    }

    private static class DeviceInfoHolder{
        private static DeviceInfo instance = new DeviceInfo();
    }

    public static DeviceInfo get() {
        return DeviceInfoHolder.instance;
    }


    public int getStartHour() {
        return mAppConfig.getIntConfig(AppConfig.ATTR_START_HOUR, START_HOUR);
    }

    public int getRepeatMin() {
        return mAppConfig.getIntConfig(AppConfig.ATTR_REPEAT, REPEAT_TIME);
    }

    public int getPeriodMin() {
        return mAppConfig.getIntConfig(AppConfig.ATTR_PERIOD, PERIOD_TIME);
    }

    public int getDelayMin() {
        return mAppConfig.getIntConfig(AppConfig.ATTR_DELAY, DELAY_TIME);
    }

    public int getEndHour() {
        return mAppConfig.getIntConfig(AppConfig.ATTR_END_HOUR, END_HOUR);
    }

    public String getSn() {
        if(BuildConfig.DEBUG) {
            return "HZ00002";
        }
        return SystemProperties.get(UaAdapter.SN, Build.SERIAL);
    }

    public String getFwVer() {
        String realVer = SystemProperties.get(UaAdapter.VERSION, DEF_VAL);
        if(!TextUtils.isEmpty(realVer)) {
            Logger.info("realVer = " + realVer);
        }
        String ver = mAppConfig.getStrConfig(AppConfig.ATTR_FW_VER, realVer);
        if(ver != null && !ver.equals(DEF_VAL)) {
            Logger.info("ver = " + ver);
            return ver;
        }
        Logger.info("android.os.Build.DISPLAY = " + Build.DISPLAY);
        return Build.DISPLAY;
    }

    public String getBrand() {
        String realBrand = SystemProperties.get(UaAdapter.BRAND, DEF_VAL);
        String brand = mAppConfig.getStrConfig(AppConfig.ATTR_MF, realBrand);
        if(brand != null && !brand.equals(DEF_VAL)) {
            Logger.info(UaAdapter.BRAND + "=" + brand);
            return brand;
        }
        Logger.info("android.os.Build.MANUFACTURER=" + Build.MANUFACTURER);
        return Build.MANUFACTURER;
    }

    public String getModel() {
        String realModel = SystemProperties.get(UaAdapter.MODEL, DEF_VAL);
        String model = mAppConfig.getStrConfig(AppConfig.ATTR_MODEL, realModel);
        if(model != null && !model.equals(DEF_VAL)) {
            Logger.info(UaAdapter.MODEL + "=" + model);
            return model;
        }
        Logger.info("Android.os.Build.MODEL=" + Build.MODEL);
        return Build.MODEL;
    }


}
